• File: sec2hms.php
  • Full Path: C:/htdocs/reeft_gps_test/css/include/sec2hms.php
  • Date Modified: 04/30/2025 7:56 AM
  • File size: 1.63 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

//======================================================================================
// Seconds to h:m:s
// http://www.laughing-buddha.net/jon/php/sec2hms/
//======================================================================================
function sec2hms ($sec, $padHours = true, $showSeconds = true) 
  {

    // holds formatted string
    $hms = "";
	
	$h_text = ' timer ';
	$m_text = ' min ';
	$s_text = ' sek ';
	
	
	$h_text = ':';
	$m_text = ':';
	$s_text = '';	
    
    // there are 3600 seconds in an hour, so if we
    // divide total seconds by 3600 and throw away
    // the remainder, we've got the number of hours
    $hours = intval(intval($sec) / 3600); 

    // add to $hms, with a leading 0 if asked for
    $hms .= ($padHours) 
          ? str_pad($hours, 2, "0", STR_PAD_LEFT). $h_text
          : $hours. $h_text;
     
    // dividing the total seconds by 60 will give us
    // the number of minutes, but we're interested in 
    // minutes past the hour: to get that, we need to 
    // divide by 60 again and keep the remainder
    $minutes = intval(($sec / 60) % 60); 

    // then add to $hms (with a leading 0 if needed)
    //$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
    $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT);

    // seconds are simple - just divide the total
    // seconds by 60 and keep the remainder
    $seconds = intval($sec % 60); 

    // add to $hms, again with a leading 0 if needed
	if ( $showSeconds == true ) {
    $hms .= $m_text . str_pad($seconds, 2, "0", STR_PAD_LEFT) . $s_text;
	}

    // done!
    return $hms;
    
  }

?>